package com.chenyun.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.extra.validation.ValidationUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chenyun.boot.datasource.page.PageUtils;
import com.chenyun.common.enums.SmsEnum;
import com.chenyun.common.enums.YesOrNoEnum;
import com.chenyun.common.exceptions.BusinessException;
import com.chenyun.message.sms.service.client.ISmsClient;
import com.chenyun.message.sms.service.client.impl.SmsClientFactory;
import com.chenyun.satoken.starter.util.SaUtil;
import com.chenyun.system.mapper.SmsSettingMapper;
import com.chenyun.system.model.SmsChannel;
import com.chenyun.system.service.ISmsChannelService;
import com.chenyun.common.annotation.Page;
import com.chenyun.common.vo.QueryPage;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 短信渠道配置
 *
 * @author: Lonni
 * @date: 2022/12/27 0027 15:21
 */
@Service
public class SmsChannelServiceImpl extends ServiceImpl<SmsSettingMapper, SmsChannel>
        implements ISmsChannelService {


    @Override
    public SmsChannel checkChannel(Long channelId) {
        SmsChannel channel = this.getById(channelId);
        if (channel == null) {
            throw new BusinessException("短信渠道不存在!");
        }

        if (channel.getStatus().equals(YesOrNoEnum.NO.ordinal())) {
            throw new BusinessException("短信渠道已禁用");
        }
        return  channel;
    }


    /**
     * 查询
     *
     * @param page
     * @return
     */
    @Override
    @Page
    public List<SmsChannel> page(QueryPage page) {
        QueryWrapper<SmsChannel> queryWrapper = PageUtils.getQueryWrapper(page);
        List<SmsChannel> list = this.list(queryWrapper);
        return list;

    }

    /**
     * 添加短信渠道
     *
     * @param smsChannel
     * @return
     */
    @Override
    public Boolean addSmsChannel(SmsChannel smsChannel) {
        ValidationUtil.validate(smsChannel);
        return this.save(smsChannel);
    }

    /**
     * 移除渠道
     *
     * @param channel
     * @return
     */
    @Override
    public Boolean removeSmsChannel(String id) {
        return this.removeById(id);
    }

    SmsChannel findByChannelName(String channelName) {
        return this.getOne(Wrappers.lambdaQuery(SmsChannel.class).eq(SmsChannel::getChannelName, channelName));
    }


    /**
     * 修改渠道
     *
     * @param smsChannel
     * @return
     */
    @Override
    public Boolean editSmsChannel(SmsChannel smsChannel) {
        SmsChannel setting = this.getById(smsChannel.getId());
        Assert.notNull(setting, "未找到指定渠道配置!");
        BeanUtil.copyProperties(smsChannel, setting);
        return this.saveOrUpdate(setting);
    }

    /**
     * 修改状态
     *
     * @param id
     * @param status
     * @return
     */
    @Override
    public Boolean changeStatus(String id, Integer status) {
        SmsChannel setting = this.getById(id);
        Assert.notNull(setting, "未找到指定渠道配置!");
        setting.setStatus(status == 0 ? 1 : 0);
        return this.saveOrUpdate(setting);
    }

    /**
     * 根据channelcode获取短信客户端
     *
     * @param channelCode
     * @return
     */
    @Override
    public ISmsClient getClient(String channelCode) {
        SmsEnum smsEnum = SmsEnum.valueOf(channelCode.toUpperCase());
        ISmsClient client = SmsClientFactory.me.getClient(SaUtil.getLoginUser().getClientId(), smsEnum);
        if (client == null) {
            throw new BusinessException(String.format("短信客户端{}未初始化!", smsEnum.getDesc()));
        }
        return client;

    }
}



